package com._54programer.goods.controller;

import com._54programer.entity.PageResult;
import com._54programer.entity.Result;
import com._54programer.entity.StatusCode;
import com._54programer.goods.pojo.Brand;
import com._54programer.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result findAll(){
        List<Brand> list = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK,"查询成功", brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/search")
    public Result findList(@RequestParam Map searchMap){
        List list = brandService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功");
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size){
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/search1/{page}/{size}")
    public Result findPage1(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size){
        Page<Brand> pageList = brandService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return
     */
    @GetMapping("/category/{category}")
    public Result  findListByCategoryName(@PathVariable String category){
        System.out.println(category);
        List<Map> brandList = brandService.findListByCategoryName(category);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }


}
